package com.ymatou.iapi.prodstock.testcase;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.prodstock.parameter.ProductActivityStockDto;
import com.ymatou.iapi.prodstock.parameter.UpdateProductActivityStockTimeBean;
import com.ymatou.iapi.prodstock.service.UpdateProductActivityStockTimeCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.database.RedisDriver;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JobQueryCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

import cn.ymatou.admin.job.parameter.JobTaskitemListBean;
/**
*更新活动时间
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UpdateProductActivityStockTime{
	private static UpdateProductActivityStockTimeBean updateproductactivitystocktimeBean;
	private static UpdateProductActivityStockTimeCall updateproductactivitystocktimeCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新活动时间");
	}

	@Before
	public void caseUp() {
		updateproductactivitystocktimeBean=new UpdateProductActivityStockTimeBean();
		updateproductactivitystocktimeCall=new UpdateProductActivityStockTimeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updateproductactivitystocktimeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductActivityStockTime_001() {
		Logger.start(true,"活动时间调整，更新活动库存表的开始和结束时间");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});

			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());

			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			//活动开始
			
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-1, YMTDateUtil.YMDTHMS_STRING);
			updateproductactivitystocktimeBean.setBeginTime(BeginTime);
			String EndTime = YMTDateUtil.getBeforeOrNextDay(1, YMTDateUtil.YMDTHMS_STRING);
			updateproductactivitystocktimeBean.setEndTime(EndTime);
			updateproductactivitystocktimeBean.setActivityID(activityId);
			updateproductactivitystocktimeBean.setProductActivityId(Integer.valueOf(aids.get(0)));
			updateproductactivitystocktimeBean.setActivityType(0);
			updateproductactivitystocktimeBean.setUserID(sellerId);
			updateproductactivitystocktimeBean.setProductID(productId);

			updateproductactivitystocktimeCall.setData(updateproductactivitystocktimeBean);
			updateproductactivitystocktimeCall.callService();
			BeginTime = YMTDateUtil.getBeforeOrNextDay(1, YMTDateUtil.YMDTHMS_STRING);
			updateproductactivitystocktimeBean.setBeginTime(BeginTime);
			EndTime = YMTDateUtil.getBeforeOrNextDay(2, YMTDateUtil.YMDTHMS_STRING);
			updateproductactivitystocktimeBean.setEndTime(EndTime);
			updateproductactivitystocktimeCall.callService();
			//updateproductactivitystocktimeCall.getReturnData();
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId);
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductActivityStockTime_002() {
		Logger.start(true,"活动时间调整，更新活动库存表的开始和结束时间-更新job");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});

			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());

			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			
			
			//验证end
			JobTaskitemListBean jobListBean = JobQueryCallService.initActivityProductStartOrEnd(productId,activityId,"END");
			List<Map> joa = JobQueryCallService.GetJobTasks(jobListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = JobQueryCallService.GetJobTasks(jobListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(1, joa.size(), "验证有end记录");
			String nexttime1=joa.get(0).get("nextTime").toString();
			Logger.debug("nexttime1:"+nexttime1);
			
			//活动开始
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-1, YMTDateUtil.YMDTHMS_STRING);
			updateproductactivitystocktimeBean.setBeginTime(BeginTime);
			String EndTime = YMTDateUtil.getBeforeOrNextDay(1, YMTDateUtil.YMDTHMS_STRING);
			updateproductactivitystocktimeBean.setEndTime(EndTime);
			updateproductactivitystocktimeBean.setActivityID(activityId);
			updateproductactivitystocktimeBean.setProductActivityId(Integer.valueOf(aids.get(0)));
			updateproductactivitystocktimeBean.setActivityType(0);
			updateproductactivitystocktimeBean.setUserID(sellerId);
			updateproductactivitystocktimeBean.setProductID(productId);

			updateproductactivitystocktimeCall.setData(updateproductactivitystocktimeBean);
			updateproductactivitystocktimeCall.callService();
			BeginTime = YMTDateUtil.getBeforeOrNextDay(1, YMTDateUtil.YMDTHMS_STRING);
			updateproductactivitystocktimeBean.setBeginTime(BeginTime);
			EndTime = YMTDateUtil.getBeforeOrNextDay(2, YMTDateUtil.YMDTHMS_STRING);
			updateproductactivitystocktimeBean.setEndTime(EndTime);
			updateproductactivitystocktimeCall.callService();
			//updateproductactivitystocktimeCall.getReturnData();
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId);
			joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			
			//验证start
			jobListBean = JobQueryCallService.initActivityProductStartOrEnd(productId,activityId,"START");
			joa = JobQueryCallService.GetJobTasks(jobListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = JobQueryCallService.GetJobTasks(jobListBean);
			}
			Logger.verifyEquals(1, joa.size(), "验证有start记录");
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			
			//验证end
			jobListBean = JobQueryCallService.initActivityProductStartOrEnd(productId,activityId,"END");
			joa = JobQueryCallService.GetJobTasks(jobListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = JobQueryCallService.GetJobTasks(jobListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(1, joa.size(), "验证有end记录");
			String nexttime2=joa.get(0).get("nextTime").toString();
			Logger.debug("nexttime2:"+nexttime2);
			Logger.verifyNotEquals(nexttime1, nexttime2, "验证时间更新");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductActivityStockTime_003() {
		Logger.start(true,"活动时间调整，更新活动库存表的开始和结束时间,验证redis的ttl");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			System.out.println(catalogs.toString());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			//添加秒杀商品数据
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			ProductManagerCallServiceV2.addSecKillStock(activityId,productId, sellerId, true);
			
			
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-1, YMTDateUtil.YMDTHMS_STRING);
			updateproductactivitystocktimeBean.setBeginTime(BeginTime);
			String EndTime = YMTDateUtil.getBeforeOrNextDay(1, YMTDateUtil.YMDTHMS_STRING);
			updateproductactivitystocktimeBean.setEndTime(EndTime);
			updateproductactivitystocktimeBean.setActivityID(activityId);
			updateproductactivitystocktimeBean.setProductActivityId(Integer.valueOf(aids.get(0)));
			updateproductactivitystocktimeBean.setActivityType(0);
			updateproductactivitystocktimeBean.setUserID(sellerId);
			updateproductactivitystocktimeBean.setProductID(productId);

			updateproductactivitystocktimeCall.setData(updateproductactivitystocktimeBean);
			updateproductactivitystocktimeCall.callService();
			Thread.sleep(500);
			RedisDriver rd = new RedisDriver("group1",EnvSetup.getSystemUrl("sentinel"));
			String key="SecStock:"+aids.get(0)+":"+catalogs.get(0);
			long ttl=rd.getTtl(key);
			Logger.comment("获取redis，ttl为："+rd.getTtl(key));
			
			BeginTime = YMTDateUtil.getBeforeOrNextDay(-1, YMTDateUtil.YMDTHMS_STRING);
			updateproductactivitystocktimeBean.setBeginTime(BeginTime);
			EndTime = YMTDateUtil.getBeforeOrNextDay(8, YMTDateUtil.YMDTHMS_STRING);
			updateproductactivitystocktimeBean.setEndTime(EndTime);
			updateproductactivitystocktimeCall.callService();
			//updateproductactivitystocktimeCall.getReturnData();
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId);
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Thread.sleep(500);
			
			Logger.comment("获取redis，key为："+key+"值为："+rd.getValue(key));
			Logger.comment("获取redis，ttl为："+rd.getTtl(key));
			long ttl1=rd.getTtl(key);
			Logger.verifyEquals((ttl1-ttl)>50, true, "ttl被修改");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
//	private void test() {
//		//活动未开始
//		String BeginTime = YMTDateUtil.getBeforeOrNextDay(1, YMTDateUtil.YMDTHMS_STRING);
//		auditproductactivitystockBean.setBeginTime(BeginTime);
//		String EndTime = YMTDateUtil.getBeforeOrNextDay(2, YMTDateUtil.YMDTHMS_STRING);
//		auditproductactivitystockBean.setEndTime(EndTime);
//		auditproductactivitystockBean.setActivityID(activityId);
//		auditproductactivitystockBean.setActivityType(0);
//		auditproductactivitystockBean.setProductActivityId(Integer.valueOf(aids.get(0)));
//		auditproductactivitystockBean.setUserID(sellerId);
//		auditproductactivitystockBean.setProductID(productId);
//		auditproductactivitystockBean.setStoreType(-1);
//		
//		ProductActivityStockDto productActivityStockDto = new ProductActivityStockDto();
//		productActivityStockDto.setActivityPrice(new BigDecimal(10));
//		productActivityStockDto.setCatalogId(catalogs.get(0));
//		productActivityStockDto.setSku(null);
//		productActivityStockDto.setStock(200);
//		ProductActivityStockDtoList.add(productActivityStockDto);
//		auditproductactivitystockBean.setFBXCatalogLockList(ProductActivityStockDtoList);
//		auditproductactivitystockCall.setData(auditproductactivitystockBean);
//		auditproductactivitystockCall.callService();
//	}
}